package cn.aws360.boot.extend.open.biz.service.impl;

import cn.aws360.boot.extend.open.api.constant.enums.RecommendApiEnum;
import cn.aws360.boot.extend.open.api.pojo.properties.OpenProperties;
import cn.aws360.boot.extend.open.api.pojo.response.ItemModel;
import cn.aws360.boot.extend.open.biz.helper.ApiHelper;
import cn.aws360.boot.extend.open.biz.service.RecommendService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author junnan
 */
@Service
@RequiredArgsConstructor
public class RecommendServiceImpl implements RecommendService {

    private final OpenProperties openProperties;

    @Override
    public ItemModel getRecNews() {
        JSONObject resultJson = ApiHelper.getApiResult4Han(RecommendApiEnum.INFO.getUrl());
        if (ObjectUtil.isNotNull(resultJson)) {
            Object dataObj = resultJson.get("data");
            return ItemModel.builder()
                    .url(resultJson.getString("imgUrl"))
                    .pic(resultJson.getString("imgUrl"))
                    .title(resultJson.getString("name"))
                    .summary(StrUtil.join(",", dataObj))
                    .build();
        }
        return null;
    }

    @Override
    public ItemModel getRecImage() {
        JSONObject resultJson = ApiHelper.getApiResult4Han(RecommendApiEnum.IMAGE.getUrl());
        if (ObjectUtil.isNotNull(resultJson)) {
            JSONObject dataJson = resultJson.getJSONObject("data");
            if (ObjectUtil.isNotNull(dataJson)) {
                return ItemModel.builder()
                        .pic(dataJson.getString("url"))
                        .title(dataJson.getString("title"))
                        .build();
            }
        }
        return null;
    }

    @Override
    public List<ItemModel> getRecMusicList() {
        JSONObject resultJson = ApiHelper.getApiResult4Cu(openProperties, RecommendApiEnum.MUSIC.getUrl());
        if (ObjectUtil.isNotNull(resultJson)) {
            JSONObject dataJson = resultJson.getJSONObject("data");
            JSONArray songsArr = dataJson.getJSONArray("songs");
            if (songsArr != null && songsArr.size() > 0) {
                List<ItemModel> modelList = new ArrayList<>();
                for (Object o : songsArr) {
                    if (o instanceof JSONObject) {
                        modelList.add(buildMusicModel((JSONObject) o));
                    }
                }
                return modelList;
            }
        }
        return null;
    }

    private ItemModel buildMusicModel(JSONObject json) {
        return ItemModel.builder()
                .url(json.getString("songUrl"))
                .pic(json.getString("imageUrl"))
                .title(json.getString("title"))
                .summary(json.getString("singer")).build();
    }

}
